package com.core.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.system.service.ISystemUserService;
import com.pojo.dto.PageDto;
import com.pojo.dto.SystemUserDto;
import com.pojo.req.user.UserInsertReq;
import com.pojo.resp.user.UserImRecordResp;
import com.pojo.resp.user.UserPageResp;
import com.utility.gruops.InsertAndUpdateGroups;
import com.utility.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理
 *     用户管理
 * @author: hw
 * @date: 2020/11/27 17:46
 */
@RestController
@RequestMapping("api/system/user")
public class UserController {

    @Autowired
    private ISystemUserService userService;



    /**
     * 查询用户列表
     *    根据多条件查询用户列表
     * @param pageDto
     * @return
     */
    @GetMapping("/page")
//    @PreAuthorize("hasAuthority('user:select')")
    public Result<IPage<UserPageResp>> selectUserPage(PageDto pageDto){
        IPage<UserPageResp> systemUserDtoPage = userService.selectUserListPage(pageDto);
        return Result.returnCodeSuccess(systemUserDtoPage);
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @GetMapping("/list")
//    @PreAuthorize("hasAuthority('user:select')")
    public Result<Page<UserImRecordResp>> selectOnlineUserList(PageDto pageDto){
        Page<UserImRecordResp> systemUserDtoPage = userService.selectOnlineUserList(pageDto);
        return Result.returnCodeSuccess(systemUserDtoPage);
    }


    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/details")
//    @PreAuthorize("hasAuthority('user:details')")
    public Result<SystemUserDto> selectUserDetails(@RequestParam("id")Long id){
        return Result.returnCodeSuccess(userService.getUserDetails(id));
    }



    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/currentDetails")
    public Result<SystemUserDto> selectCurrentDetails(){
        return Result.returnCodeSuccess(userService.getCurrentDetails());
    }

    /**
     * 新增用户
     * @param userInsertReq
     * @return
     */
    @PostMapping("/insert")
//    @PreAuthorize("hasAuthority('user:add')")
    public Result insertUser(@RequestBody @Validated UserInsertReq userInsertReq){
        userService.insertUser(userInsertReq);
        return Result.returnCodeSuccess();
    }

    /**
     * 编辑用户
     * @param systemUserDto
     * @return
     */
    @PostMapping("/update")
//    @PreAuthorize("hasAuthority('user:update')")
     public Result updateUser(@RequestBody @Validated(InsertAndUpdateGroups.class) SystemUserDto systemUserDto){
     userService.updateUser(systemUserDto);
     return Result.returnCodeSuccess();
     }


    /**
     * 删除用户
     * @param systemUserDto
     * @return
     */
    @PostMapping("/delete")
//    @PreAuthorize("hasAuthority('user:remove')")
    public Result deleteUser(@RequestBody SystemUserDto systemUserDto){
        userService.deleteUser(systemUserDto);
        return Result.returnCodeSuccess();
    }

    /**
     * 批量删除用户
     * @param systemUserDto
     * @return
     */
    @PostMapping("batch/delete")
//    @PreAuthorize("hasAuthority('user:remove')")
    public Result batchDeleteUser(@RequestBody SystemUserDto systemUserDto){
        userService.batchDeleteUser(systemUserDto);
        return Result.returnCodeSuccess();
    }

    /**
     * 编辑用户状态
     * @param systemUserDto
     * @return
     */
    @PostMapping("/status/resetStatus")
//    @PreAuthorize("hasAuthority('user:resetStatus')")
    public Result statusStart(@RequestBody SystemUserDto systemUserDto){
        userService.updateUserStatus(systemUserDto);
        return Result.returnCodeSuccess();
    }



    /**
     * 重置密码
     * @param systemUserDto
     * @return
     */
    @PostMapping("/password/reset")
//    @PreAuthorize("hasAuthority('user:resetPass')")
    public Result resetPassword(@RequestBody SystemUserDto systemUserDto){
        userService.passwordUpdate(systemUserDto);
        return Result.returnCodeSuccess();
    }


    /**
     * 退出登录,并清除redis中的token
     **/
    @PostMapping("/remove")
    public Result removeToken(@RequestParam(value  = "accessToken") String accessToken,@RequestParam(value  = "userName") String userName){
       userService.removeToken(accessToken,userName);
       return Result.returnCodeSuccess();
    }


    /**
     * 据用户和菜单id 查询该用户该菜单的权限
     * @param templateId 菜单id
     * @return
     */
    @GetMapping("/userTemplatePermission")
    public Result<List<String>> userTemplatePermission(@RequestParam(value  = "templateId") Long templateId){
        return Result.returnCodeSuccess(userService.selectUserPermission(Long.valueOf(templateId)));
    }


}
